﻿/*************************************************************************
	Copyright (C) 2009-2010 欣风工作室    
	All rights reserved
	
	CLR版本:			2.0.50727.4961
	命名空间名称:		街头篮球自动购买道具
	文件名:				JpgColor2BlackAndWhite
	创建时间:			2011/7/3 13:51:25
	作者:				我烤
	QQ:					8230707
	E-Mail:				xfgzskof@163.com
	
	修改日志:
	
*************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace 街头篮球自动购买道具
{
    class JpgColor2BlackAndWhite
    {
        /// <summary>
        /// 快速的将彩色图像变成黑白图像-目前仅适用于jpg格式的图像 
        /// </summary>
        /// <param name="tmpImage">彩色图像</param>
        /// <returns>返回的黑白图像</returns>
        public static Bitmap QuickWhiteAndBlack(Image tmpImage)
        {

            // 从文件创建Bitmap对象
            Bitmap bmp = new Bitmap(tmpImage);

            // 将Bitmap锁定到系统内存中
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            // 获得BitmapData
            System.Drawing.Imaging.BitmapData bmpData =
                bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                bmp.PixelFormat);

            // 位图中第一个像素数据的地址。它也可以看成是位图中的第一个扫描行
            IntPtr ptr = bmpData.Scan0;

            // 将Bitmap对象的信息存放到byte数组中
            // 假设位图中一个像素包含3byte,也就是24bit
            int bytes = bmp.Width * bmp.Height * 3;
            byte[] rgbValues = new byte[bytes];

            //复制GRB信息到byte数组
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

            // 根据Y=0.299*R+0.114*G+0.587B,Y为亮度
            for (int counter = 0; counter < rgbValues.Length; counter += 3)
            {
                byte value = (byte)(rgbValues[counter] * 0.299 + rgbValues[counter + 2] * 0.114 + rgbValues[counter + 1] * 0.587);
                rgbValues[counter] = value;
                rgbValues[counter + 1] = value;
                rgbValues[counter + 2] = value;
            }

            //将更改过的byte[]拷贝到原位图
            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

            // 解锁位图
            bmp.UnlockBits(bmpData);
            return bmp;

        }

        /// <summary>
        /// 将彩色图片变成黑白色的照片
        /// </summary>
        /// <param name="image">原来图片</param>
        /// <returns>返回的黑白照片</returns>
        public static Bitmap WhiteAndBlack(Image tmpImage)
        {
            Bitmap bmp = new Bitmap(tmpImage);

            //原来图片的长度
            int width = bmp.Width;
            //原来图片的高度
            int height = bmp.Height;
            //改变色素
            //横坐标
            for (int x = 0; x < width; x++)
            {
                //纵坐标
                for (int y = 0; y < height; y++)
                {
                    //获得坐标(x,y)颜色
                    Color color = bmp.GetPixel(x, y);
                    //获得该颜色下的黑白色
                    //int value = (color.R + color.G + color.B) / 3;
                    //设置颜色
                    if (color.R + color.G + color.B > 255 * 3 / 2)
                    {
                        // 黑色
                        bmp.SetPixel(x, y, Color.FromArgb(255, 255, 255));
                    }
                    else
                    {
                        // 白色
                        bmp.SetPixel(x, y, Color.FromArgb(0, 0, 0));
                    }
                    //bmp.SetPixel(x, y, Color.FromArgb(value, value, value));
                }
            }
            return bmp;
        }

        public static Bitmap Relief(Image SImage, int p)
        {
            int Height = SImage.Height;
            int Width = SImage.Width;
            Bitmap bitmap = new Bitmap(Width, Height);
            Bitmap MyBitmap = (Bitmap)SImage;
            Color pixel1, pixel2;
            for (int x = 0; x < Width - 1; x++)
            {
                for (int y = 0; y < Height - 1; y++)
                {
                    int r = 0, g = 0, b = 0;
                    pixel1 = MyBitmap.GetPixel(x, y);
                    pixel2 = MyBitmap.GetPixel(x + 1, y + 1);
                    r = pixel1.R - pixel2.R + p;
                    g = pixel1.G - pixel2.G + p;
                    b = pixel1.B - pixel2.B + p;
                    if (r > 255)
                        r = 255;
                    if (r < 0)
                        r = 0;
                    if (g > 255)
                        g = 255;
                    if (g < 0)
                        g = 0;
                    if (b > 255)
                        b = 255;
                    if (b < 0)
                        b = 0;
                    bitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
                }
            }
            return bitmap;
        }

        public static Bitmap ToGray(Image tmpImage, int mode)
        {
            Bitmap bmp = new Bitmap(tmpImage);

            if (bmp == null)
            {
                return null;
            }

            int w = bmp.Width;
            int h = bmp.Height;
            try
            {
                byte newColor = 0;
                BitmapData srcData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                unsafe
                {
                    byte* p = (byte*)srcData.Scan0.ToPointer();
                    for (int y = 0; y < h; y++)
                    {
                        for (int x = 0; x < w; x++)
                        {

                            if (mode == 0)　// 加权平均
                            {
                                newColor = (byte)((float)p[0] * 0.114f + (float)p[1] * 0.587f + (float)p[2] * 0.299f);
                            }
                            else　　　　// 算数平均
                            {
                                newColor = (byte)((float)(p[0] + p[1] + p[2]) / 3.0f);
                            }
                            p[0] = newColor;
                            p[1] = newColor;
                            p[2] = newColor;

                            p += 3;
                        }
                        p += srcData.Stride - w * 3;
                    }
                    bmp.UnlockBits(srcData);
                    return bmp;
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 对比两张图片是否一致
        /// </summary>
        /// <param name="bmp1">图片1</param>
        /// <param name="bmp2">图片2</param>
        /// <returns></returns>
        public static bool CompareBmp(Bitmap bmp1, Bitmap bmp2)
        {
            int width = bmp1.Width;
            int height = bmp1.Height;

            bool result = true;

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    result = (bmp1.GetPixel(x, y) == bmp2.GetPixel(x, y));
                    if (result == false)
                    {
                        return result;
                    }
                }
            }

            return result;
        }
    }
}
